package com.salesmonitoring.accounts.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.swingx.JXDatePicker;

import com.salesmonitoring.accounts.controller.AdminController;
import com.salesmonitoring.accounts.controller.MarketingAssistantController;
import com.salesmonitoring.accounts.model.User;
import com.salesmonitoring.accounts.view.AdminUI.ReportRowListener;
import com.salesmonitoring.model.RepairShop;
import com.salesmonitoring.model.Transaction;
import com.salesmonitoring.view.LoginScreen;

@SuppressWarnings("serial")
public class AccountOfficerUI extends JFrame implements ActionListener {

	private JPanel acctOfficerPanel;
	private JLabel lblSMS, lblFirstnameRole;
	private JLabel lblAccountOfficer;
	private JComboBox accountOfficerComboBox;
	private String[] names, shops;
	private ArrayList<User> users;
	private ArrayList<Transaction> reports;
	private JLabel lblMonth;
	private JComboBox monthComboBox;

	private String[] monthStrings = { "", "Jan", "Feb", "Mar", "Apr", "May","Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
//	private String[] yearlist ={"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", };
	private String[] yearlist ={"", "2013", "2012", "2011", "2010", "2009", "2009", "2007", "2006", "2005", "2014", "2003", "2002", "2001", };
	
	private int curentYear = Calendar.getInstance().get(Calendar.YEAR);
	private JLabel lblYear;
	public int reportsNumber = 2;
	public int reportsColumnsNumber = 16;
	private JComboBox yearComboBox;
	private JButton searchButton;
	private JButton addReportButton;
	private JButton editReportButton;
	private JButton deleteReportButton;
	private JButton exportButton;
	private JTable reportsTable;
	private JScrollPane reportsTableScrollPanel;
	private JButton logoutButton;
	private AbstractButton acctSettingButton;
	private JTabbedPane tabPanel;
	private JPanel reportsTabPanel;
	private AccountSettingsUI acctSettingsPopup;

	public AccountOfficerUI() {

		AdminController adminController = AdminController.getInstance();

		this.users = adminController.getAllUsers();
		this.names = adminController.getAllAONames();
		this.shops = adminController.getAllRepairShops();
		
		initialize();
	}

	public void initialize() {

		setTitle("Bank of Makati | Sales Monitoring System");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1125, 625);

		acctOfficerPanel = new JPanel();
		acctOfficerPanel.setLayout(null);
		setContentPane(acctOfficerPanel);

		lblSMS = new JLabel("Bank of Makati");
		lblSMS.setFont(new Font("Lucida Grande", Font.PLAIN, 22));
		lblSMS.setBounds(10, 8, 260, 29);
		acctOfficerPanel.add(lblSMS);

//		acctSettingButton = new JButton("Account Settings");
//		acctSettingButton.addActionListener(this);
//		acctSettingButton.setBounds(888, 36, 139, 29);
//		acctOfficerPanel.add(acctSettingButton);

		lblFirstnameRole = new JLabel("Kiko (Account Officer)");
		lblFirstnameRole.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFirstnameRole.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		lblFirstnameRole.setBounds(740, 12, 360, 26);
		acctOfficerPanel.add(lblFirstnameRole);

		logoutButton = new JButton("Logout");
		logoutButton.addActionListener(this);
		logoutButton.setBounds(1010, 35, 88, 29);
		acctOfficerPanel.add(logoutButton);;

		tabPanel = new JTabbedPane(JTabbedPane.TOP);
		tabPanel.setBounds(0, 65, 1109, 532);
		acctOfficerPanel.add(tabPanel);
		
		acctSettingsPopup = new AccountSettingsUI();

		initReportsTab();
		tabPanel.addTab("Reports", null, reportsTabPanel, null);
	}

	public void initReportsTab() {

		reportsTabPanel = new JPanel();
		reportsTabPanel.setLayout(null);

		// ---Account officer

		lblAccountOfficer = new JLabel("Account Officer");
		lblAccountOfficer.setBounds(10, 13, 109, 16);
		reportsTabPanel.add(lblAccountOfficer);

		accountOfficerComboBox = new JComboBox(names);
		accountOfficerComboBox.setBounds(109, 9, 157, 27);
		reportsTabPanel.add(accountOfficerComboBox);

		// ---Month

		lblMonth = new JLabel("Month");
		lblMonth.setBounds(278, 13, 41, 16);
		reportsTabPanel.add(lblMonth);

		monthComboBox = new JComboBox(monthStrings);
		monthComboBox.setBounds(317, 9, 93, 27);
		reportsTabPanel.add(monthComboBox);

		// ---Year

		lblYear = new JLabel("Year");
		lblYear.setBounds(422, 13, 70, 16);
		reportsTabPanel.add(lblYear);

		yearComboBox = new JComboBox(yearlist);
		yearComboBox.setBounds(451, 9, 93, 27);
		reportsTabPanel.add(yearComboBox);

		// --
		searchButton = new JButton("Search");
		searchButton.setBounds(550, 9, 81, 27);
		searchButton.addActionListener(this);
		reportsTabPanel.add(searchButton);
		
		exportButton = new JButton("Export to PDF");
		exportButton.setBounds(978, 9, 119, 27);
		exportButton.setEnabled(false);
		reportsTabPanel.add(exportButton);
		
	}

	public void initReportsTable() {

		ReportTableModel reportTableModel = new ReportTableModel(reports.size());
		reportsTable = new JTable(reportTableModel);
		reportsTable.setFillsViewportHeight(true);
		reportsTable.setShowGrid(true);
		reportsTable.setShowHorizontalLines(true);
		reportsTable.setShowVerticalLines(true);
		reportsTable.getTableHeader().setReorderingAllowed(false);

		ListSelectionModel selectionModel = reportsTable.getSelectionModel();
		selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		selectionModel.addListSelectionListener(new ReportRowListener(this));

		if(reports.isEmpty()) {
			exportButton.setEnabled(false);
		} else {
			exportButton.setEnabled(true);
		}
		
		for(int row = 0; row < reports.size(); row++) {
			
			reportsTable.setValueAt(reports.get(row).getDateRequested(), row, 0); //Date Requested - Date
			reportsTable.setValueAt(reports.get(row).getOldCustomer(), row, 1); //Original Customer - String
			reportsTable.setValueAt(reports.get(row).getAccountNumber(), row, 2); //Account # - int
			reportsTable.setValueAt(reports.get(row).getNewCustomer(), row, 3); //New Buyer - String
			reportsTable.setValueAt(reports.get(row).getMotorWarehouse(), row, 4); //Warehouse - String
			reportsTable.setValueAt(reports.get(row).getDateSold(), row, 5);//Date Sold - Date
			reportsTable.setValueAt(reports.get(row).getMotorModel(), row, 6); 	//Model - String
			reportsTable.setValueAt(reports.get(row).getMotorEngine(), row, 7); //Engine - int
			reportsTable.setValueAt(reports.get(row).getMotorChassis(), row, 8); //Chassis - int
			reportsTable.setValueAt(reports.get(row).getDateRepo(), row, 9);//Date Repo - Date
			reportsTable.setValueAt(reports.get(row).getMotorPlate(), row, 10);//Plate # - String
			reportsTable.setValueAt(reports.get(row).getAsmf(), row, 11);	//ASMF # - int
			reportsTable.setValueAt(reports.get(row).getMotorClass(), row, 12);//Class - String
			reportsTable.setValueAt(reports.get(row).getRs(), row, 13);// RS Code - String
		}

		// This Scroll Panel holds the Table - But table is displayed on click
		// of "Search"
		reportsTableScrollPanel = new JScrollPane(reportsTable);
		reportsTableScrollPanel.setBounds(10, 42, 1087, 438);
		reportsTabPanel.add(reportsTableScrollPanel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if (e.getActionCommand().equals("Search")) {

			String officer = accountOfficerComboBox.getSelectedItem().toString();
			String month = monthComboBox.getSelectedItem().toString();
			String year = yearComboBox.getSelectedItem().toString();
			
			MarketingAssistantController marketingAssistantController = MarketingAssistantController.getInstance();
			
			reports = marketingAssistantController.getReports(officer, month, year);
			
			initReportsTable();
			
		} else if(e.getActionCommand().equals("Account Settings")) {
			
			if(!checkForActiveWindows()) {
				acctSettingsPopup.setLocationRelativeTo(null);
				acctSettingsPopup.setVisible(true);
			}
		} else {
			super.dispose();
			LoginScreen loginScreen = new LoginScreen();
			loginScreen.setVisible(true);
			loginScreen.setLocationRelativeTo(null);
		}
	}
	
	public boolean checkForActiveWindows() {
		
		boolean activeWindows = false;
		
		if(this.acctSettingsPopup.isVisible()) {
			activeWindows = true;
		} else {
			activeWindows = false;
		}
		
		return activeWindows;
	}
	
	public void setCurrentUserName(String name) {
		lblFirstnameRole.setText(name);
	}

	class ReportRowListener implements ListSelectionListener {

		AccountOfficerUI readRow;
		JTable table;
		private String originalCustName;

		public ReportRowListener(AccountOfficerUI rar) {
			readRow = rar;
			table = readRow.reportsTable;
		}

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (!e.getValueIsAdjusting()) {
				ListSelectionModel model = table.getSelectionModel();
				int lead = model.getLeadSelectionIndex();
				getSelectedRowDateRequested(lead);
				// edit.setEnabled(true);
			}
		}

		private void getSelectedRowDateRequested(int rowIndex) {

			Object o = table.getValueAt(rowIndex, 1);
			originalCustName = "" + o.toString();
		}
	}
}
